package springapp.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import springapp.domain.Suggestion;
import springapp.domain.SuggestionItem;

@Controller
@RequestMapping("/suggest")
public class SuggestionController {
	@RequestMapping(value = "/test", method = RequestMethod.GET, produces = "application/json")
	public @ResponseBody SuggestionItem[] doTest(@RequestParam String name){
		return new SuggestionItem[]{new SuggestionItem(), new SuggestionItem()};
	}
	
	@RequestMapping(value = "/test2", method = RequestMethod.GET, produces = "application/json")
	public @ResponseBody String doTest2(@RequestParam String name){
		return "Response from ajax " + name;
	}
	
	@RequestMapping(value = "/test4", method = RequestMethod.GET, produces = "application/json")
	public @ResponseBody Suggestion doTest4(@RequestParam String query){
		Suggestion result = new Suggestion();
		result.setQuery(query);
		if (query != null && query.startsWith("en-22")){
			result.setSuggestions(new String[]{"en-222", "en-223"});
			result.setData(new String[]{"en-222", "en-223"});
			return result;
		}
		
		if (query != null && query.startsWith("en")){
			result.setSuggestions(new String[]{"en-1", "en-2", "en-222", "en-223"});
			result.setData(new String[]{"en-1", "en-2", "en-222", "en-223"});
			return result;
		}
		
		return result;
	}
	
	@RequestMapping(value = "/test3", method = RequestMethod.GET, produces = "application/json")
	public @ResponseBody SuggestionItem[] doTest3(@RequestParam(required = false) String term){
		SuggestionItem userChoice = new SuggestionItem();
		userChoice.setName(term);
		userChoice.setValue(term);
		return new SuggestionItem[]{userChoice, new SuggestionItem(), new SuggestionItem()};
	}
	
	@RequestMapping(value = "/autotest", method = RequestMethod.GET)
    public String autotest() {
        return "autotest";
    }
}
